import { menuItems } from './menu';

Component({
  data: {
    active: 0,
    menuItems: menuItems,
  },
  lifetimes: {
    attached: function () {
      setTimeout(() => {
        this.showMenu();
      }, 0);
    },
  },
  methods: {
    onChange(event: any) {
        this.setData({
          active: event.detail
        });
        wx.switchTab({
          url: this.data.menuItems[event.detail].url.startsWith('/')
            ? this.data.menuItems[event.detail].url
            : `/${this.data.menuItems[event.detail].url}`,
        });
        this.showMenu();
    },
    showMenu() {
      const page = getCurrentPages().pop();
      const route = page ? page.route.split('?')[0] : '';
      const active = this.data.menuItems.findIndex(
        (item: any) =>
          (item.url.startsWith('/') ? item.url.substr(1) : item.url) ===
          `${route}`,
      );
      this.setData({ active });
    },
  }
});